package yxy.game.pm2.command.union;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.stat.StatHandle;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;

/**
 * 联盟 联盟主界面
 */
@MessageType(proto = Command.UnionMain, classtype = UnionMessage.UnionMainRequest.class)
public class CMD_UnionMain extends Command<UnionMessage.UnionMainRequest> {

    @Override
    public void execute(MessageSource messageSource, UnionMessage.UnionMainRequest request) {
        String playerCode = messageSource.getTag();

        UnionMessage.UnionMainResponse.Builder response = UnionMessage.UnionMainResponse.newBuilder();

        UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
        Result result = new UnionHandle().checkPlayerUnion(entry);
        if (result.equals(Result.UnionOut)) {
            MessageEmitterHandler.getInstance().send(messageSource, new UnionHandle().hotList(playerCode));
            return;
        }
        if (!result.equals(Result.UnionIn)) {
            send(messageSource, response, result);
            return;
        }

        //活跃的三天不上线的盟主自动转让位置
        changeLeader(entry);

        // 封装消息
        UnionBean union = entry.getUnionBean();
        response = new UnionHandle().unionMain(union, playerCode);
        send(messageSource, response, Result.Success);
    }


    private void send(MessageSource messageSource, UnionMessage.UnionMainResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    private void changeLeader(UnionPlayerEntry entry) {
        try {
            UnionBean union = entry.getUnionBean();
            String leader = union.getLeader();
            PlayerBean bean = PlayerBean.get(leader);
            if (bean == null) return;

            if (union.getMemberList().size() <= 0 && union.getViceList().size() <= 0) return;

            DateTime updateTime = bean.getUpdateTime();
            DateTime now = DateTime.now();
            int betweenDays = ActiveUtil.getBetweenDays(updateTime, now);


            //5天盟主仍未登陆过，则选择活跃度高并且战力最高的玩家成为领主
            if (betweenDays >= 5) {
                long power = 0;
                String code = "";
                for (String playercode : union.getMemberList()) {
                    //todo 修改登录时间，3天后自动清除缓存
                    StatHandle.stat_用户登录状态更新(playercode);
                    PlayerBean player = PlayerBean.get(playercode);
                    DateTime time = player.getUpdateTime();
                    if (ActiveUtil.getBetweenDays(time, now) >= 5) continue;//5天没上线的没资格成为盟主
                    PlayerBean playerBean = PlayerBean.get(playercode);
                    if (playerBean.getPower() > power) {
                        power = playerBean.getPower();
                        code = playercode;
                    }
                }

                for (String playercode : union.getViceList()) {
                    //todo 修改登录时间，3天后自动清除缓存
                    StatHandle.stat_用户登录状态更新(playercode);
                    PlayerBean player = PlayerBean.get(playercode);
                    DateTime time = player.getUpdateTime();
                    if (ActiveUtil.getBetweenDays(time, now) >= 5) continue;//5天没上线的没资格成为盟主
                    PlayerBean playerBean = PlayerBean.get(playercode);
                    if (playerBean.getPower() > power) {
                        power = playerBean.getPower();
                        code = playercode;
                    }
                }

                //更换盟主
                if (StringUtils.isNotEmpty(code)) {

                    if (union.getViceList().contains(code)) {
                        union.getViceList().remove(code);
                        union.setLeader(code);
                    }

                    if (union.getMemberList().contains(code)) {
                        union.getMemberList().remove(code);
                        union.setLeader(code);
                    }
                    union.getMemberList().add(leader);
                }

                union.set();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}